//
//  Detail_fanggeViewController.m
//  盘盈贷1.2
//
//  Created by iOS on 15/3/6.
//  Copyright (c) 2015年 iOS. All rights reserved.
//

#import "Detail_fanggeViewController.h"
#import "CollectionViewCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "List.h"
#import "HMSegmentedControl.h"
#import "Zaiquan.h"
#import "ZaiquanTableViewCell.h"
#import "ListViewController.h"
@interface Detail_fanggeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)UICollectionView *collectionView;

@property (nonatomic, retain)NSString *api;
@property (nonatomic, retain) NSString *apiString;
@property (nonatomic, retain)NSString *apiString1;
@property (nonatomic, retain)HMSegmentedControl *segmentedControl;
@end

@implementation Detail_fanggeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"产品列表";
        self.view.backgroundColor = [UIColor whiteColor];
        
      
        self.array = [NSMutableArray array];
        
        self.array1 = [NSMutableArray array];
        self.arr = [NSMutableArray array];
        
      self.navigationItem.hidesBackButton = YES;
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-liebiao.png"] style:UIBarButtonItemStyleDone target:self action:@selector(Clicked:)];
         self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"刷新" style:UIBarButtonItemStyleDone target:self action:@selector(fresh:)];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(110, 110);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 10, 25);
    flowLayout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height + 100) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    //self.collectionView.backgroundColor = [UIColor grayColor];
    self.collectionView.contentSize = CGSizeMake(width, height);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.view addSubview:self.collectionView];
    
    self.segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"天盈宝", @"供应链融资", @"众筹拍房", @"债权转让"]];
    [self.segmentedControl setFrame:CGRectMake(5, 64, self.view.frame.size.width, 40)];
    [self.segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    // [segmentedControl setSegmentEdgeInset:UIEdgeInsetsMake(10, 50, 0, 6)];
    [self.segmentedControl setSelectionIndicatorHeight:4.0f];
    
    [self.segmentedControl setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed];
    [self.segmentedControl setContentVerticalAlignment:UIControlContentVerticalAlignmentFill];
    self.segmentedControl.selectionIndicatorColor = [UIColor orangeColor];
    [self.segmentedControl setTintColor:[UIColor orangeColor]];
    self.segmentedControl.tintColor = [UIColor orangeColor];
    [self.segmentedControl setTag:1];
    [self.view addSubview:self.segmentedControl];
    NSString *str = @"http://www.cloudpeng.com:8102/product/lendlist?productcateid=1";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSDictionary *dic in responseObject) {
            List *model = [[List alloc] initWithDictionary:dic];
            [self.array addObject:model];
            
        }
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"~~~~~~~%@",error);
    }];
    

}
- (void)segmentAction:(HMSegmentedControl *)segmentedControl

{
    
    switch (segmentedControl.selectedIndex) {
        case 0:
            self.apiString = [self.api stringByAppendingFormat:@":8102/product/lendlist?productcateid=1"];
            self.array = [NSMutableArray array];
            [self LoadingData];
            break;
        case 1:
            self.apiString = [self.api stringByAppendingFormat:@":8102/product/lendlist?productcateid=3"];
            self.array = [NSMutableArray array];
            [self LoadingData];
            break;
        case 2:
            self.apiString = [self.api stringByAppendingFormat:@":8102/product/lendlist?productcateid=2"];
            self.array = [NSMutableArray array];
            [self LoadingData];
            break;
        case 3:
            self.apiString = [self.api stringByAppendingFormat:@":8102/product/transferlist"];
            
            [self LoadingData];
            break;
    }
}
- (void)LoadingData
{
    if (self.apiString == nil) {
        self.apiString = [self.api stringByAppendingFormat:@":8102/product/lendlist?productcateid=1"];
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:self.apiString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            for (NSDictionary *dic in responseObject) {
                List *model = [[List alloc] initWithDictionary:dic];
                [self.arr addObject:model];
                //NSLog(@"%@", self.arr);
                
            }
            [self.collectionView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"___________%@",error);
        }];
        
        [self.collectionView reloadData];
        
    }
    if (self.segmentedControl.selectedIndex == 0 || self.segmentedControl.selectedIndex == 1 || self.segmentedControl.selectedIndex == 2) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:self.apiString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            for (NSDictionary *dic in responseObject) {
                List *model = [[List alloc] initWithDictionary:dic];
                [self.array addObject:model];
            }
            [self.collectionView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"~~~~~~~%@",error);
        }];
        
    }else {
        self.apiString1 = [self.api stringByAppendingFormat:@":8102/product/transferlist"];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:self.apiString1 parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            for (NSDictionary *dic1 in responseObject) {
                Zaiquan *model1 = [[Zaiquan alloc] initWithDictionary:dic1];
                [self.array1 addObject:model1];
                
            }
            [self.collectionView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"~~~~~~~%@",error);
        }];
        
    }
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.array count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    NSString *a = [[self.array objectAtIndex:indexPath.row] BidAmount];
    float b = [a floatValue];
    NSString *c = [[self.array objectAtIndex:indexPath.row] Amount];
    float d = [c floatValue];
    CGFloat result1 = (b / d) * 100;
    CGFloat result = b / d;
    cell.progress.centerLabel.text = [NSString stringWithFormat:@"%.0f", result1];
    cell.progress.percentage = result;
    cell.nameTitle.text = [[self.array objectAtIndex:indexPath.row] Title];
    cell.jiekuan.text = [NSString stringWithFormat:@"借款 :%@.0", [[self.array objectAtIndex:indexPath.row] Amount]];
    cell.lilv.text = [NSString stringWithFormat:@"利率 :%@.0",[[self.array objectAtIndex:indexPath.row] LoanRate]];
    NSString *str = [[self.array objectAtIndex:indexPath.row] PublishTime];
    NSString *str2= [str substringWithRange:NSMakeRange(0,10)];
    cell.time.text = [NSString stringWithFormat:@"时间 :%@",str2];
    
    cell.qixian.text = [NSString stringWithFormat:@"期限 :%@月",[[self.array objectAtIndex:indexPath.row] Limit]];
   // cell.backgroundColor = [UIColor greenColor];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}

- (void)Clicked:(UIBarButtonItem *)button
{
    ListViewController *list = [[ListViewController alloc] init];
    [self.navigationController pushViewController:list animated:YES];
    
}
- (void)refresh:(UIBarButtonItem *)button
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
